﻿Public Class PriorityTypes

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        '
        ' Close the Priority Types form
        '
        Me.Close()
    End Sub

    Private Sub PriorityTypes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshDataGrid()
    End Sub

    Private Sub RefreshDataGrid()
        ' Populate the Datagrid from values in the db
        dgPriorityTypes.AutoGenerateColumns = False
        dgPriorityTypes.DataSource = clsUIBase.RetrievePriorities
        dgPriorityTypes.AutoResizeColumns()
    End Sub

    Private Sub btnAddPriorityIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPriorityIssue.Click
        ' Prompt the user for the new Priority Type
        Dim desc As String = InputBox("Please enter a new Priority Type Description: ", "EZ Ticket - New Priority", "Enter Description here..")

        ' Ensure the user enters a priority type, add the priority to the db and refresh the datagrid to show the results
        If desc <> "" Then
            clsUIBase.AddPriorityType(desc)
            RefreshDataGrid()
        Else
            MessageBox.Show("You cannot enter a blank description", "EZ Ticket - Input Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnDeletePriorityIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeletePriorityIssue.Click
        ' Ensure a row is selected
        If dgPriorityTypes.SelectedCells.Count > 0 Then
            ' Determine the id of the currently selected priority
            Dim id As Integer = dgPriorityTypes.Rows(dgPriorityTypes.CurrentRow.Index).Cells("priorityid").Value()

            ' if the id is not null, delete it from the db and refresh the datagrid to show the results
            If id <> Nothing Then
                clsUIBase.DeletePriorityType(id)
                RefreshDataGrid()
            End If
        End If
    End Sub

    Private Sub dgPriorityTypes_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgPriorityTypes.SelectionChanged
        ' Ensure a value is selected in the datagrid before the delete button will function
        If dgPriorityTypes.SelectedCells.Count > 0 Then
            Me.btnDeletePriorityIssue.Enabled = True
        Else
            Me.btnDeletePriorityIssue.Enabled = False
        End If
    End Sub
End Class